//
//  YH_ChangeAboutUsViewController.m
//  YinHang
//
//  Created by apple on 14-9-20.
//  Copyright (c) 2014年 孙彦鹏. All rights reserved.
//

#import "YH_ChangeAboutUsViewController.h"

@interface YH_ChangeAboutUsViewController()<ASIHTTPRequestDelegate>{
    
    MBProgressHUD *postHud;
    
    ASIFormDataRequest *Request_;//发送请求
    //参数
    NSString *action;
    NSDictionary *paramsDict;
}

@end

@implementation YH_ChangeAboutUsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBarHidden = NO;
   
    
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 30)];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = - 10;
    if (ios7) {
        self.navigationItem.leftBarButtonItems = @[negativeSpacer,leftBarButtonItem];
    }else{
        self.navigationItem.leftBarButtonItems = @[leftBarButtonItem];
    }


        [self.view addSubview:_aboutUsTitle];
    
    
    
    if ([_state isEqualToString:@"修改"]) {
        self.navigationItem.title = @"修改信息";
        
        _aboutUsTitle.text = _TitleStr;
        _aboutUsContent.text = _ContentStr;
       
        
    } else {
        self.navigationItem.title = @"添加信息";
        
        _aboutUsTitle.text = nil;
        _aboutUsContent.text = nil;
       
    }

    paramsDict = [NSDictionary dictionary];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)back {
  
    [self.navigationController popViewControllerAnimated:YES];
}
//提交
- (IBAction)tiJiaoBtnClick:(id)sender {
    
    if (_aboutUsTitle.text.length == 0 && _aboutUsContent.text.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请填写完整" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    [self postRequstAddOrAlert];
    //[self.navigationController popViewControllerAnimated:YES];
   
}

-(void)postRequstAddOrAlert{
    
    if ([_state isEqualToString:@"修改"]) { //修改
        action = @"aboutupdate";
        if (!postHud || postHud.hidden == YES) {
            // 提示正在加载数据...
            postHud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            postHud.labelText = @"正在发布，请稍后...";
        }
        
        Request_ = [[ASIFormDataRequest alloc]initWithURL:[NSURL URLWithString:API_STR]];
        Request_.requestMethod = @"POST";
        // 设置超时时间
        Request_.timeOutSeconds = 60;
        Request_.delegate = self;
        // 设置参数
        paramsDict = @{@"id":_selectId,@"uid":UNAME,@"title":_aboutUsTitle.text,@"content":_aboutUsContent.text};//uid会员名;kname:网址名称;kweb:网址
        NSString *paramsStr = [NSString JSONString:paramsDict];
        NSString *result = [NSString stringWithFormat:TEMPLATE_PARAMSTR, action,paramsStr];
        
        NSData *paramsData = [result toGBKEncodingData];
        Request_.postBody = [paramsData mutableCopy];
        
        [Request_ startAsynchronous];

        //[_delegate didSaveData];
    } else { //添加
        action = @"aboutadd";
        if (!postHud || postHud.hidden == YES) {
            // 提示正在加载数据...
            postHud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            postHud.labelText = @"正在发布，请稍后...";
        }
        
        Request_ = [[ASIFormDataRequest alloc]initWithURL:[NSURL URLWithString:API_STR]];
        Request_.requestMethod = @"POST";
        // 设置超时时间
        Request_.timeOutSeconds = 60;
        Request_.delegate = self;
        // 设置参数
        paramsDict = @{@"uid":UNAME,@"title":_aboutUsTitle.text,@"content":_aboutUsContent.text};//uid会员名;
        NSString *paramsStr = [NSString JSONString:paramsDict];
        NSString *result = [NSString stringWithFormat:TEMPLATE_PARAMSTR, action,paramsStr];
        
        NSData *paramsData = [result toGBKEncodingData];
        Request_.postBody = [paramsData mutableCopy];
        
        [Request_ startAsynchronous];

       
        
        //[_delegate didSaveData];
    }
    
}

#pragma mark - ASIHttpRequestDelegate method
- (void)requestFinished:(ASIHTTPRequest *)request {
    
    NSError *err;
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[request.responseString dataForUTF8] options:NSJSONReadingAllowFragments error:&err];
    NSLog(@"%@",result);
    // 1. 是否解析有错误
    if (!err) {
        
        // 2. 判断错误码
        NSInteger errorCode = [result[@"errorCode"]integerValue];
        if (errorCode == 0) {
            [postHud hide:YES];
            
            
            // 提示发布成功
            [UIView animateWithDuration:0 animations:^{
                if ([_state isEqualToString:@"添加"]) {
                    [SVProgressHUD showSuccessWithStatus:@"添加成功！"];
                    [_delegate didSaveData];
                    [self.navigationController popViewControllerAnimated:YES];
                }else{
                    [SVProgressHUD showSuccessWithStatus:@"修改成功！"];
                    [_delegate didSaveData];
                    [self.navigationController popViewControllerAnimated:YES];
                }
                
                
            } completion:^(BOOL finished) {
                
            }];
        } else {
            [postHud hide:YES];
            [SVProgressHUD showErrorWithStatus:result[@"errorMsg"]];
        }
    } else {
        [postHud hide:YES];
        [SVProgressHUD showErrorWithStatus:@"数据解析出错！"];
    }
}
- (void)requestFailed:(ASIHTTPRequest *)request {
    
    [SVProgressHUD showErrorWithStatus:@"服务器无法访问，请稍后再试!"];
}

-(void)dealloc{
    [Request_ cancel];
    postHud = nil;
}

@end
